"use strict";
this.InputValueHider = function () {};

this.InputValueHider.prototype = (function () {
    var init, thread, onConfigurationLoaded, fields, initField, onRedraw,
            selectors;

    init = function () {
        fields = [];
        selectors = [];
    };

    thread = function () {};

    onRedraw = function (webLayout, details) {
        var i, elements, j, k, include;
        for (i = selectors.length - 1; i >= 0; i -= 1) {
            elements = webLayout.xPath(selectors[i]);
            for (j = elements.length - 1; j >= 0; j -= 1) {
                include = true;
                for (k = fields.length - 1; k >= 0; k -= 1) {
                    if (fields[k] === elements[j]) {
                        include = false;
                    }
                }
                if (include) {
                    fields.push(elements[j]);
                    initField(webLayout, elements[j]);
                }
            }
        }
    };

    onConfigurationLoaded = function (webLayout, config) {
        var i;
        if (config[this.NAME] === undefined) {
            return;
        }
        selectors = config[this.NAME];
        for (i = selectors.length - 1; i >= 0; i -= 1) {
            fields = fields.concat(webLayout.xPath(selectors[i]));
        }
        for (i = fields.length - 1; i >= 0; i -= 1) {
            initField(webLayout, fields[i]);
        }
    };

    initField = function (webLayout, field) {
        var value;
        value = field.value;
        webLayout.addEventListener(field, 'focus', function () {
            if (field.value === value) {
                field.value = '';
            }
        });
        webLayout.addEventListener(field, 'blur', function () {
            if (field.value === '') {
                field.value = value;
            }
        });
    };

    return {
        NAME: 'InputValueHider',
        init: init,
        thread: thread,
        onConfigurationLoaded: onConfigurationLoaded,
        onRedraw: onRedraw
    };
}());